package com.example.onlinereadingsystem.demos.web.util;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description http工具类
 * @Author ljm
 * @Date 2024/12/26
 */
public class HttpUtil {

    // 错误返回
    public static void errorResponse(HttpServletResponse response, String msg, int code) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Credentials", "false");
        String res = JSONObject.toJSONString(Result.error(code, msg));
        response.getWriter().write(res);
    }
}
